package com.vf.cloud.paas.app.master.api.itester.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.common.domain.ApiTestCustom;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/itester/custom")
public class ITesterCustomController {
	
	
	@Tx
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<ApiTestCustom>> list(HttpServletRequest request, @RequestBody ApiTestCustom apiTestCustom) {
		String _sql="select * from api_test_custom where project_id=? ";
		if(!StrKit.isBlank(apiTestCustom.getName())) {
			_sql+=" and name like '%"+apiTestCustom.getName()+"%' ";
		}
		_sql+=" order by sort asc ";
		List<ApiTestCustom> list=ApiTestCustom.dao.find(_sql,apiTestCustom.getProjectId());
		return R.ok(list);
	}
	
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<String> save(HttpServletRequest request, @RequestBody ApiTestCustom apiTestCustom) {
		if (StrKit.isBlank(apiTestCustom.getId())) {
			apiTestCustom.setId(UuidUtil.getUUID());
			if (apiTestCustom.save())
				return R.ok();
		} else {
			if (apiTestCustom.update())
				return R.ok();
		}
		return R.failed();
	}

	@Tx
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody ApiTestCustom apiTestCustom) {
		try {
			if (apiTestCustom.delete()) {
				return R.ok();
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
		return R.failed();
	}

}
